// Main.java

import dao.AttendanceDAO;
import dao.DepartmentDAO;
import dao.EmployeeDAO;
import pojo.Attendance;
import pojo.Department;
import pojo.Employee;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        EmployeeDAO employeeDAO = new EmployeeDAO();
        DepartmentDAO departmentDAO = new DepartmentDAO();
        AttendanceDAO attendanceDAO = new AttendanceDAO();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("******** 企业员工信息管理系统 ********");
            System.out.println("1. 添加员工信息");
            System.out.println("2. 修改员工信息");
            System.out.println("3. 删除员工信息");
            System.out.println("4. 查询员工信息");
            System.out.println("5. 添加部门信息");
            System.out.println("6. 修改部门信息");
            System.out.println("7. 删除部门信息");
            System.out.println("8. 查询所有部门信息");
            System.out.println("9. 录入考勤信息");
            System.out.println("10. 查询考勤信息及统计");
            System.out.println("0. 退出系统");
            System.out.print("请输入对应数字：");

            String input = scanner.nextLine();
            int choice = -1;
            try {
                choice = Integer.parseInt(input);
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入数字！");
                continue;
            }

            switch (choice) {
                case 1:
                    // 添加员工信息
                    try {
                        System.out.print("请输入员工编号：");
                        String empId = scanner.nextLine();
                        System.out.print("请输入员工姓名：");
                        String empName = scanner.nextLine();
                        System.out.print("请输入员工性别：");
                        String gender = scanner.nextLine();
                        System.out.print("请输入员工年龄：");
                        int age = Integer.parseInt(scanner.nextLine());
                        System.out.print("请输入所属部门：");
                        String dept = scanner.nextLine();
                        System.out.print("请输入员工职位：");
                        String position = scanner.nextLine();
                        System.out.print("请输入入职日期(yyyy-MM-dd)：");
                        String dateStr = scanner.nextLine();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date joinDate = sdf.parse(dateStr);
                        System.out.print("请输入联系电话：");
                        String phone = scanner.nextLine();
                        System.out.print("请输入电子邮箱：");
                        String email = scanner.nextLine();

                        Employee emp = new Employee(empId, empName, gender, age, dept, position, joinDate, phone, email);
                        if (employeeDAO.addEmployee(emp)) {
                            System.out.println("员工添加成功！");
                        } else {
                            System.out.println("员工添加失败！");
                        }
                    } catch (Exception e) {
                        System.out.println("输入有误：" + e.getMessage());
                    }
                    break;
                case 2:
                    // 修改员工信息
                    try {
                        System.out.print("请输入要修改的员工编号：");
                        String empId = scanner.nextLine();
                        Employee emp = employeeDAO.getEmployeeById(empId);
                        if (emp == null) {
                            System.out.println("未找到该员工！");
                            break;
                        }
                        System.out.println("当前信息：" + emp);
                        System.out.print("请输入新的部门（留空表示不修改）：");
                        String newDept = scanner.nextLine();
                        if (!newDept.isEmpty()) {
                            emp.setDepartment(newDept);
                        }
                        System.out.print("请输入新的职位（留空表示不修改）：");
                        String newPosition = scanner.nextLine();
                        if (!newPosition.isEmpty()) {
                            emp.setPosition(newPosition);
                        }
                        System.out.print("请输入新的联系电话（留空表示不修改）：");
                        String newPhone = scanner.nextLine();
                        if (!newPhone.isEmpty()) {
                            emp.setPhone(newPhone);
                        }
                        // 可根据需要增加其他字段修改
                        if (employeeDAO.updateEmployee(emp)) {
                            System.out.println("员工信息修改成功！");
                        } else {
                            System.out.println("员工信息修改失败！");
                        }
                    } catch (Exception e) {
                        System.out.println("输入有误：" + e.getMessage());
                    }
                    break;
                case 3:
                    // 删除员工信息
                    System.out.print("请输入要删除的员工编号：");
                    String empIdToDelete = scanner.nextLine();
                    if (employeeDAO.deleteEmployee(empIdToDelete)) {
                        System.out.println("员工删除成功！");
                    } else {
                        System.out.println("员工删除失败！");
                    }
                    break;
                case 4:
                    // 查询员工信息
                    System.out.println("查询方式：1. 按员工编号  2. 按姓名  3. 按部门");
                    String option = scanner.nextLine();
                    String condition = "";
                    if ("1".equals(option)) {
                        System.out.print("请输入员工编号：");
                        String id = scanner.nextLine();
                        condition = "employee_id = '" + id + "'";
                    } else if ("2".equals(option)) {
                        System.out.print("请输入员工姓名：");
                        String name = scanner.nextLine();
                        condition = "name LIKE '%" + name + "%'";
                    } else if ("3".equals(option)) {
                        System.out.print("请输入部门名称：");
                        String department = scanner.nextLine();
                        condition = "department = '" + department + "'";
                    } else {
                        System.out.println("无效选项！");
                        break;
                    }
                    List<Employee> empList = employeeDAO.queryEmployeesByCondition(condition);
                    if (empList.isEmpty()) {
                        System.out.println("未查询到员工记录！");
                    } else {
                        for (Employee e : empList) {
                            System.out.println(e);
                        }
                    }
                    break;
                case 5:
                    // 添加部门信息
                    System.out.print("请输入部门名称：");
                    String departmentName = scanner.nextLine();
                    System.out.print("请输入部门描述：");
                    String description = scanner.nextLine();
                    Department department = new Department(departmentName, description);
                    if (departmentDAO.addDepartment(department)) {
                        System.out.println("部门添加成功！");
                    } else {
                        System.out.println("部门添加失败！");
                    }
                    break;
                case 6:
                    // 修改部门信息
                    System.out.print("请输入要修改的部门名称：");
                    String deptNameToModify = scanner.nextLine();
                    System.out.print("请输入新的部门描述：");
                    String newDescription = scanner.nextLine();
                    Department deptToUpdate = new Department(deptNameToModify, newDescription);
                    if (departmentDAO.updateDepartment(deptToUpdate)) {
                        System.out.println("部门信息修改成功！");
                    } else {
                        System.out.println("部门信息修改失败！");
                    }
                    break;
                case 7:
                    // 删除部门信息
                    System.out.print("请输入要删除的部门名称：");
                    String deptNameToDelete = scanner.nextLine();
                    if (departmentDAO.deleteDepartment(deptNameToDelete)) {
                        System.out.println("部门删除成功！");
                    } else {
                        System.out.println("部门删除失败！");
                    }
                    break;
                case 8:
                    // 查询所有部门信息
                    List<Department> deptList = departmentDAO.getAllDepartments();
                    if (deptList.isEmpty()) {
                        System.out.println("暂无部门记录！");
                    } else {
                        for (Department d : deptList) {
                            System.out.println(d);
                        }
                    }
                    break;
                case 9:
                    // 录入考勤信息
                    try {
                        System.out.print("请输入员工编号：");
                        String attEmpId = scanner.nextLine();
                        System.out.print("请输入考勤日期(yyyy-MM-dd)：");
                        String attDateStr = scanner.nextLine();
                        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
                        Date attDate = sdf2.parse(attDateStr);
                        System.out.print("请输入考勤状态（例如：正常、迟到、早退、旷工、请假）：");
                        String status = scanner.nextLine();
                        Attendance att = new Attendance(attEmpId, attDate, status);
                        if (attendanceDAO.addAttendance(att)) {
                            System.out.println("考勤信息录入成功！");
                        } else {
                            System.out.println("考勤信息录入失败！");
                        }
                    } catch (Exception e) {
                        System.out.println("输入有误：" + e.getMessage());
                    }
                    break;
                case 10:
                    // 查询考勤信息及统计
                    try {
                        System.out.print("请输入员工编号：");
                        String attEmpIdQuery = scanner.nextLine();
                        System.out.print("请输入开始日期(yyyy-MM-dd)：");
                        String startDateStr = scanner.nextLine();
                        System.out.print("请输入结束日期(yyyy-MM-dd)：");
                        String endDateStr = scanner.nextLine();
                        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
                        Date startDate = sdf3.parse(startDateStr);
                        Date endDate = sdf3.parse(endDateStr);
                        List<Attendance> attList = attendanceDAO.queryAttendance(attEmpIdQuery, startDate, endDate);
                        if (attList.isEmpty()) {
                            System.out.println("该时间段内无考勤记录！");
                        } else {
                            for (Attendance a : attList) {
                                System.out.println(a);
                            }
                            Map<String, Integer> stats = attendanceDAO.getAttendanceStatistics(attEmpIdQuery, startDate, endDate);
                            System.out.println("迟到次数：" + stats.get("迟到"));
                            System.out.println("旷工天数：" + stats.get("旷工"));
                        }
                    } catch (Exception e) {
                        System.out.println("输入有误：" + e.getMessage());
                    }
                    break;
                case 0:
                    System.out.println("退出系统。");
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
            System.out.println("按回车键继续...");
            scanner.nextLine();
        }
    }
}
